package com.naughty.userlogin02.controller;
import com.alibaba.fastjson.JSON;
import com.naughty.userlogin02.bean.QueryInfo;
import com.naughty.userlogin02.bean.Stu;
import com.naughty.userlogin02.dao.StuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
@RestController
public class StuController {

    @Autowired
    StuDao stuDao;
    @CrossOrigin

    @RequestMapping("/allStu")
    public String getStuList(QueryInfo queryInfo){
        System.out.println("111");
        System.out.println(queryInfo);
        int numbers = stuDao.getStuCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Stu> stu = stuDao.getAllStu("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",stu);
        System.out.println("总条数111："+numbers);
        String users_json = JSON.toJSONString(res);
        return users_json;
    }


    @RequestMapping("/stuState")
    public String updateStuState(@RequestParam("id") Integer  id,
                                  @RequestParam("state") Boolean state){
        int i = stuDao.updateState(id, state);
        System.out.println("用户编号:"+id);
        System.out.println("用户状态:"+state);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/addStu")
    public String addStu(@RequestBody Stu stu){
        System.out.println(stu);
        stu.setRole("用户");
        stu.setState(false);
        int i = stuDao.addStu(stu);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/getUpdate1")
    public String getUpdateStu(int id){
        System.out.println("编号:"+id);
        Stu updateStu = stuDao.getUpdateStu(id);
        String users_json = JSON.toJSONString(updateStu);
        return users_json;
    }

    @RequestMapping("/editStu")
    public String editStu(@RequestBody Stu stu){
        System.out.println(stu);
        int i = stuDao.editStu(stu);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/deleteStu")
    public String deleteStu(int id){
        System.out.println(id);
        int i = stuDao.deleteStu(id);
        String str = i >0?"success":"error";
        return str;
    }

}
